﻿using System;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.Administration.Models;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.SystemFacade.Extensions;
using Fuse8.ContentManagementFramework.Web.MvcPowered;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.Common;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsModuleController : CmsAdminController
    {
        private readonly IEntityBusinessService<CmsModule, int> ModuleService = new BusinessServiceFacade<CmsModule, int>();
        
        public ActionResult Index()
        {
            return View(ModuleService.FindAll());
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View(new CmsModuleModel());
        }

        [HttpPost]
        public ActionResult Add(CmsModuleModel model)
        {
            AdditionalValidation(model);
            if (ModelState.IsValid)
            {
                var module = new CmsModule();
                BindModelToEntity(model, module);

                ModuleService.Add(module);

                return RedirectToAction("index");
            }

            return View(model);
        }

        private void BindModelToEntity(CmsModuleModel model, CmsModule module)
        {
            module.ModuleName = model.ModuleName;
            module.ModuleDescription = model.ModuleDescription;
            module.SiteCategory = model.SiteCategory;
            module.SiteGroupId = model.SiteGroupId;
            module.SiteId = model.SiteId;
                             

            var partialModel = new CmsMvcPartialModel()
                                   {
                                       PartialAdministrationViewName = model.PartialAdministrationViewName
                                   };
            if (model.ControlType == PageControlTypeKind.MvcPartial)
            {
                partialModel.PartialViewName = model.PartialViewName;
                partialModel.ActionController = null;
                partialModel.ActionName = null;
            }
            else if (model.ControlType == PageControlTypeKind.MvcAction)
            {
                partialModel.ActionName = model.ActionName;
                partialModel.ActionController = model.ActionController;
                partialModel.PartialViewName = null;
            }

            if (!string.IsNullOrEmpty(model.PartialModelType))
            {
                Type type = Type.GetType(model.PartialModelType, true, true);
                partialModel.PartialModelType = TypeInterpreter.GetTypeName(type);
            }

            module.ModuleViewParameters = partialModel.StoreMe();            
        }

        private void AdditionalValidation(CmsModuleModel model)
        {
            if (model.ControlType == PageControlTypeKind.MvcPartial && string.IsNullOrEmpty(model.PartialViewName))
            {
                ModelState.AddModelError("PartialViewName", "Please enter partial view name"); 
            }

            if (model.ControlType == PageControlTypeKind.MvcAction && (string.IsNullOrEmpty(model.ActionController) || string.IsNullOrEmpty(model.ActionName)))
            {
                ModelState.AddModelError("ActionController", "Please enter action and controller name");
            }
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var module = ModuleService.FindOne(id);

            return View(BindEntityToModel(module));
        }

        [HttpPost]
        public ActionResult Edit(CmsModuleModel model)
        {
            AdditionalValidation(model);
            if (ModelState.IsValid)
            {
                var module = ModuleService.FindOne(model.Id);
                BindModelToEntity(model, module);

                ModuleService.Save(module);

                return RedirectToAction("index");
            }

            return View(model);
        }

        private CmsModuleModel BindEntityToModel(CmsModule module)
        {
            var partialModel = module.ModuleViewParameters.DigUpFromString<CmsMvcPartialModel>();
            var model = new CmsModuleModel()
                            {
                                ModuleName = module.ModuleName,
                                ModuleDescription = module.ModuleDescription,
                                Id = module.Id,
                                SiteGroupId = module.SiteGroupId,
                                SiteId = module.SiteId,
                                PartialAdministrationViewName = partialModel.PartialAdministrationViewName,
                                PartialModelType = partialModel.PartialModelType,
                                PartialViewName = partialModel.PartialViewName,
                                ActionController = partialModel.ActionController,
                                ActionName = partialModel.ActionName,
                                ControlType = string.IsNullOrEmpty(partialModel.PartialViewName)?PageControlTypeKind.MvcAction : PageControlTypeKind.MvcPartial
                            };

            return model;
        }
    }
}